package com.mao.service.impl.productstock;

import java.io.OutputStream;
import java.util.List;

import jxl.Workbook;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.NumberFormat;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mao.action.jsonvo.searchcenter.JsonVoSearchProductStock;
import com.mao.action.jsonvo.searchcenter.JsonVoSearchProfit;
import com.mao.dao.product.ProductClassDao;
import com.mao.dao.product.ProductDao;
import com.mao.dao.productstock.ProductStockDao;
import com.mao.dao.searchvoucher.SearchVoucherDao;
import com.mao.model.product.Product;
import com.mao.service.impl.searchvoucher.SearchVoucherCondition;
import com.mao.service.productstock.IProductStockService;

@Service("productStockService")
public class ProductStockServiceImpl implements IProductStockService {

	private ProductStockDao productStockDao;
	
	private ProductClassDao productClassDao;
	
	private SearchVoucherDao searchVoucherDao;
	
	private ProductDao productDao;

	public ProductStockDao getProductStockDao() {
		return productStockDao;
	}
	
	@Autowired
	public void setProductStockDao(ProductStockDao productStockDao) {
		this.productStockDao = productStockDao;
	}
	public	List<JsonVoSearchProductStock> findStocksByPro(SearchVoucherCondition seachVoucherCondition) {
		return productStockDao.findStocksByPro(seachVoucherCondition);
	}
	
	public Integer findStockByProWareHouseDWR(Long productId,Long wareHouseId){
		Product pro = productDao.getProductById(productId);
		SearchVoucherCondition searchVoucherCondition = new SearchVoucherCondition();
		searchVoucherCondition.setPro(pro);
		searchVoucherCondition.setWhId(wareHouseId);
		List<JsonVoSearchProductStock> list = findStocksByPro(searchVoucherCondition);
		Integer stock = list.get(0).getStock();
		// 判断是否要减掉初始库存，如果当前出库商品的初始库存仓库不等于当前出库的仓库则需要减掉
//		if(pro.getInitWareHouse() != null){
//			if(!searchVoucherCondition.getWhId().equals(pro.getInitWareHouse().getId())){
//				stock = stock - pro.getInitStock();
//			}
//		}
		return stock;
	}
	
	@Override
	public List<JsonVoSearchProductStock> searchStockByPcId(ProductStockCondition psd,Integer page, Integer rows) {
		return productStockDao.searchStockByPcId(psd,page,rows);
	}
	
	public Integer searchStockByPcIdCount(ProductStockCondition psd) {
		return productStockDao.searchStockByPcIdCount(psd);
	}
	
	private WritableCellFormat getCellStyle(WritableCellFormat wcf, int flag)
			throws WriteException {
		wcf.setBorder(Border.ALL, BorderLineStyle.THIN);
		// 设置单元格内容对齐方式
		if (flag == 0)
			wcf.setAlignment(jxl.format.Alignment.LEFT);
		else if (flag == 1)
			wcf.setAlignment(jxl.format.Alignment.RIGHT);
		else
			wcf.setAlignment(jxl.format.Alignment.CENTRE);
		// 设置单元格内容为垂直居中
		wcf.setVerticalAlignment(VerticalAlignment.CENTRE);
		return wcf;
	}
	
	//导出剩余库存信息
	public void exportRemainStock(ProductStockCondition psc, OutputStream os) {
		List<JsonVoSearchProductStock> list = productStockDao.searchStockByPcId(psc,1,Integer.MAX_VALUE);
		if (list != null && list.size() > 0) {
			String title = "（所有商品类别）库存余量";
			if(psc != null)
				title = psc.getName()+"库存余量";
			try {
				// 创建文件
				WritableWorkbook book = Workbook.createWorkbook(os);
				// 生成名为“第一页”的工作表，参数0表示这是第一页
				WritableSheet sheet = book.createSheet("第一页 ", 0);
				sheet.mergeCells(0, 0, 8, 0);
				WritableFont font = new WritableFont(WritableFont.TIMES, 12, WritableFont.BOLD);
				WritableCellFormat leftFormat = new WritableCellFormat(font);// 标题靠左
				leftFormat = getCellStyle(leftFormat, 0);
				WritableCellFormat rightFormat = new WritableCellFormat(font);// 标题靠右
				rightFormat = getCellStyle(rightFormat, 1);

				WritableCellFormat format = new WritableCellFormat();// 内容靠左
				format = getCellStyle(format, 0);

				NumberFormat numFmt = new NumberFormat("#.00");
				WritableCellFormat format2 = new WritableCellFormat(numFmt);// 内容靠右
				format2 = getCellStyle(format2, 1);

				WritableCellFormat centerFormat = new WritableCellFormat(font);// 标题居中
				centerFormat = getCellStyle(centerFormat, 3);
				WritableCellFormat format3 = new WritableCellFormat();// 日期居中
				format3 = getCellStyle(format3, 3);

				// 设置每列的宽度
				sheet.setColumnView(0, 10);
				sheet.setColumnView(1, 30);
				sheet.setColumnView(2, 15);
				sheet.setColumnView(3, 15);
				sheet.setColumnView(4, 22);
				sheet.setColumnView(5, 22);
				sheet.setColumnView(6, 22);
				sheet.setColumnView(7, 22);
				sheet.setColumnView(8, 22);

				sheet.addCell(new Label(0, 0, title, centerFormat));
				sheet.addCell(new Label(0, 1, "商品编号", leftFormat));
				sheet.addCell(new Label(1, 1, "商品名称", leftFormat));
				sheet.addCell(new Label(2, 1, "商品规格", leftFormat));
				sheet.addCell(new Label(3, 1, "商品类别", leftFormat));
				sheet.addCell(new Label(4, 1, "进货数量", rightFormat));
				sheet.addCell(new Label(5, 1, "进货退货数量", rightFormat));
				sheet.addCell(new Label(6, 1, "销售数量", rightFormat));
				sheet.addCell(new Label(7, 1, "销售退货数量", rightFormat));
				sheet.addCell(new Label(8, 1, "库存余量", rightFormat));

				for (int i = 0; i < list.size(); i++) {
					JsonVoSearchProductStock pro = list.get(i);
					int j = 0;
					sheet.addCell(new Label(j, i + 2, pro.getProNo(), format));
					sheet.addCell(new Label(j+1, i + 2, pro.getProName(), format));
					sheet.addCell(new Label(j+2, i + 2, pro.getSpec(), format));
					sheet.addCell(new Label(j+3, i + 2, pro.getProClassName(), format));
					sheet.addCell(new jxl.write.Number(j+4, i + 2, pro.getPurchaseNum().doubleValue(), format2));
					sheet.addCell(new jxl.write.Number(j+5, i + 2, pro.getrPurchaseNum().doubleValue(), format2));
					sheet.addCell(new jxl.write.Number(j+6, i + 2, pro.getSaleNum().doubleValue(), format2));
					sheet.addCell(new jxl.write.Number(j+7, i + 2, pro.getrSaleNum().doubleValue(), format2));
					sheet.addCell(new jxl.write.Number(j+8, i + 2, pro.getStock().doubleValue(), format2));
					
				}
				
				book.write();
				book.close();
			} catch (Exception e) {
				System.out.println(e);
			}
		} else {
			try {
				WritableWorkbook book = Workbook.createWorkbook(os);
				WritableSheet sheet = book.createSheet("第一页 ", 0);
				sheet.addCell(new Label(1, 0, "没有数据"));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	//导出商品盈利情况
	public void exportProfit(SearchVoucherCondition searchVoucherCondition, Integer page, Integer rows, OutputStream os) {
		List<JsonVoSearchProfit> list = searchVoucherDao.searchProfit(searchVoucherCondition, page, rows);
		if (list != null && list.size() > 0) {
			String title = searchVoucherCondition.getTitle();
			
			try {
				// 创建文件
				WritableWorkbook book = Workbook.createWorkbook(os);
				// 生成名为“第一页”的工作表，参数0表示这是第一页
				WritableSheet sheet = book.createSheet("第一页 ", 0);
				sheet.mergeCells(0, 0, 9, 0);
				WritableFont font = new WritableFont(WritableFont.TIMES, 12, WritableFont.BOLD);
				WritableCellFormat leftFormat = new WritableCellFormat(font);// 标题靠左
				leftFormat = getCellStyle(leftFormat, 0);
				WritableCellFormat rightFormat = new WritableCellFormat(font);// 标题靠右
				rightFormat = getCellStyle(rightFormat, 1);

				WritableCellFormat format = new WritableCellFormat();// 内容靠左
				format = getCellStyle(format, 0);

				NumberFormat numFmt = new NumberFormat("#.00");
				WritableCellFormat format2 = new WritableCellFormat(numFmt);// 内容靠右
				format2 = getCellStyle(format2, 1);

				WritableCellFormat centerFormat = new WritableCellFormat(font);// 标题居中
				centerFormat = getCellStyle(centerFormat, 3);
				WritableCellFormat format3 = new WritableCellFormat();// 日期居中
				format3 = getCellStyle(format3, 3);

				// 设置每列的宽度
				sheet.setColumnView(0, 10);
				sheet.setColumnView(1, 30);
				sheet.setColumnView(2, 15);
				sheet.setColumnView(3, 15);
				sheet.setColumnView(4, 18);
				sheet.setColumnView(5, 18);
				sheet.setColumnView(6, 22);
				sheet.setColumnView(7, 18);
				sheet.setColumnView(8, 22);
				sheet.setColumnView(9, 22);

				sheet.addCell(new Label(0, 0, title, centerFormat));
				sheet.addCell(new Label(0, 1, "商品编号", leftFormat));
				sheet.addCell(new Label(1, 1, "商品名称", leftFormat));
				sheet.addCell(new Label(2, 1, "商品规格", leftFormat));
				sheet.addCell(new Label(3, 1, "商品类别", leftFormat));
				sheet.addCell(new Label(4, 1, "销售数量", rightFormat));
				sheet.addCell(new Label(5, 1, "成本均价（元）", rightFormat));
				sheet.addCell(new Label(6, 1, "成本金额（元）", rightFormat));
				sheet.addCell(new Label(7, 1, "销售均价（元）", rightFormat));
				sheet.addCell(new Label(8, 1, "销售金额（元）", rightFormat));
				sheet.addCell(new Label(9, 1, "盈利金额（元）", rightFormat));

				for (int i = 0; i < list.size(); i++) {
					JsonVoSearchProfit pro = list.get(i);
					int j = 0;
					sheet.addCell(new Label(j, i + 2, pro.getProNo(), format));
					sheet.addCell(new Label(j+1, i + 2, pro.getProName(), format));
					sheet.addCell(new Label(j+2, i + 2, pro.getProSpec(), format));
					sheet.addCell(new Label(j+3, i + 2, pro.getProClassName(), format));
					sheet.addCell(new jxl.write.Number(j+4, i + 2, pro.getSaleNum().doubleValue(), format2));
					sheet.addCell(new jxl.write.Number(j+5, i + 2, pro.getAvgCost().doubleValue(), format2));
					sheet.addCell(new jxl.write.Number(j+6, i + 2, pro.getCostPrice().doubleValue(), format2));
					sheet.addCell(new jxl.write.Number(j+7, i + 2, pro.getAvgSalePrice().doubleValue(), format2));
					sheet.addCell(new jxl.write.Number(j+8, i + 2, pro.getSalePrice().doubleValue(), format2));
					sheet.addCell(new jxl.write.Number(j+9, i + 2, pro.getProfitPrice().doubleValue(), format2));
				}
				
				book.write();
				book.close();
			} catch (Exception e) {
				System.out.println(e);
			}
		} else {
			try {
				WritableWorkbook book = Workbook.createWorkbook(os);
				WritableSheet sheet = book.createSheet("第一页 ", 0);
				sheet.addCell(new Label(1, 0, "没有数据"));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	public ProductClassDao getProductClassDao() {
		return productClassDao;
	}
	@Autowired
	public void setProductClassDao(ProductClassDao productClassDao) {
		this.productClassDao = productClassDao;
	}

	public SearchVoucherDao getSearchVoucherDao() {
		return searchVoucherDao;
	}
	@Autowired
	public void setSearchVoucherDao(SearchVoucherDao searchVoucherDao) {
		this.searchVoucherDao = searchVoucherDao;
	}

	public ProductDao getProductDao() {
		return productDao;
	}
	@Autowired
	public void setProductDao(ProductDao productDao) {
		this.productDao = productDao;
	}


}
